package ssm.blog.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ssm.blog.entity.*;
import ssm.blog.service.*;
import ssm.blog.util.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: lyon
 * @description: 后台主界面控制层
 * @author: Lyon
 * @create: 2018-05-20 19:10
 **/
@Controller
@RequestMapping("/admin")
public class AdminHomeController {
    @Resource
    BlogTypeService blogTypeService;
    @Resource
    BlogService blogService;
    @Resource
    QuotationService quotationService;
    @Resource
    AlbumService albumService;
    @Resource
    ContactMsgService contactMsgService;
    @RequestMapping("/home")
    String home(Model model){
        User currentUser=(User)SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        model.addAttribute("currentUser",currentUser);
        return "interface/admin/home";
    }
    @RequestMapping("/welcome")
    String welcome(Model model){
        User currentUser=(User)SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        model.addAttribute("currentUser",currentUser);
        Integer blogTypeCount=blogTypeService.count();
        Integer blogCount=blogService.count();
        Integer quotationsCount=quotationService.count();
        Integer albumsCount=albumService.count();
        Integer contactMsgCount=contactMsgService.count();
        model.addAttribute("blogTypeCount",blogTypeCount);
        model.addAttribute("blogCount",blogCount);
        model.addAttribute("quotationsCount",quotationsCount);
        model.addAttribute("albumsCount",albumsCount);
        model.addAttribute("contactMsgCount",contactMsgCount);
        return "interface/admin/welcome";
    }
    @RequestMapping("/blogTypeList")
    String listBlogType(HttpServletResponse response, Model model,
                        @RequestParam(defaultValue = "")String keyword,
                        @RequestParam(defaultValue = "0")Integer pageNum,
                        @RequestParam(defaultValue = "10") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        List<BlogType> blogTypes=blogTypeService.listByKeyword(keyword);
        PageInfo<BlogType> pageInfo=new PageInfo<>(blogTypes);
        model.addAttribute("blogTypes",blogTypes);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("keyword",keyword);
        return  "interface/admin/blogTypeList";
    }
    @RequestMapping("blogList")
    String listBlog(Model model,
                    @RequestParam(defaultValue = "-1")Integer type_id,
                    @RequestParam(defaultValue = "")String keyword,
                    @RequestParam(defaultValue = "0")Integer pageNum,
                    @RequestParam(defaultValue = "10") Integer pageSize)throws Exception{
        List<Blog> blogs;
        if(type_id.equals(-1)){
            PageHelper.startPage(pageNum,pageSize);
            blogs=blogService.listByKeyword(keyword);
        }else{
            PageHelper.startPage(pageNum,pageSize);
            blogs=blogService.listByKeywordAndTypeId(keyword,type_id);
        }
        PageInfo<Blog> pageInfo=new PageInfo<>(blogs);
        model.addAttribute("blogs",blogs);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("keyword",keyword);
        List<BlogType>blogTypes=blogTypeService.list();
        model.addAttribute("blogTypes",blogTypes);
        model.addAttribute("type_id",type_id);
        return "interface/admin/blogList";
    }
    @RequestMapping("/quotationsList")
    String quotationsList(Model model,
                          @RequestParam(defaultValue = "")String keyword,
                          @RequestParam(defaultValue = "0")Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Quotation> quotations=quotationService.listByKeyword(keyword);
        PageInfo<Quotation> pageInfo=new PageInfo<>(quotations);
        model.addAttribute("quotations",quotations);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("keyword",keyword);
        return "interface/admin/quotationsList";
    }
    @RequestMapping("/albumsList")
    String albumsList(Model model,
                          @RequestParam(defaultValue = "")String keyword,
                          @RequestParam(defaultValue = "0")Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Album> albums=albumService.listByKeyword(keyword);
        PageInfo<Album> pageInfo=new PageInfo<>(albums);
        model.addAttribute("albums",albums);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("keyword",keyword);
        return "interface/admin/albumsList";
    }
    @RequestMapping("/contactMsgList")
    String contactMsgList(Model model,
                          @RequestParam(defaultValue = "")String keyword,
                          @RequestParam(defaultValue = "0")Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ContactMsg> msgs=contactMsgService.listByKeyword(keyword);
        PageInfo<ContactMsg> pageInfo=new PageInfo<>(msgs);
        model.addAttribute("msgs",msgs);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("keyword",keyword);
        return "interface/admin/contactMsgList";
    }
}
